package com.ruoyi.web.core.config;

import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.AjaxResult;

import java.util.List;
import java.util.Map;

@Aspect
@Component
public class SwaggerAop {

    /**
     * AjaxResult.class
     */
    private static final String code = "code";

    private static final String codeName = "响应编码";

    private static final String msg = "msg";

    private static final String msgName = "响应消息";

    private static final String data = "data";

    private static final String dataName = "响应数据";

    /**
     * 表格分页数据对象
     */
    private static final String total= "total";

    private static final String totalName= "总记录数";

    private static final String rows= "rows";

    private static final String rowsName= "列表数据";


    @Pointcut(value = "execution(public * springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapDocumentation(..))")
    public void point(){ }

    @Around("point()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Swagger swagger = (Swagger) proceedingJoinPoint.proceed();
        Map<String, Model> returnValue = swagger.getDefinitions() ;
        returnValue.putAll(ApiResponseBodyReader.extra_cache);

        List<String> extra_key = ApiResponseBodyReader.extra_key; //获取所有@ApiResponseMap 的 modleName
        if(extra_key != null && extra_key.size()>0) {
            for (int i = 0; i < extra_key.size(); i++) {
                swaggerHelp(extra_key.get(i),returnValue);
            }
        }
        return swagger;
    }

    private void swaggerHelp(String extraKey,Map<String, Model> returnValue){
        Class<?> resultType = ApiResponseBodyReader.resultType.get(extraKey);  //获取方法的返回值类型
        System.out.println(resultType == AjaxResult.class);

        //返回类型为AjaxResult
        if(resultType == AjaxResult.class) {
            //定义包装类Model
            ModelImpl model = new ModelImpl();
            //1、添加code
            Property codeProperty = new StringProperty();
            codeProperty.setDescription(codeName);
            model.property(code,codeProperty);
            //2、添加msg
            Property msgProperty = new StringProperty();
            msgProperty.setDescription(msgName);
            model.property(msg,msgProperty);
            //3、添加data
            Property dataProperty = new RefProperty(extraKey+ApiResponseBodyReader.swaggerFuffix);
            dataProperty.setDescription(dataName);
            model.property(data,dataProperty);

            returnValue.put(extraKey,model);
        } else if(resultType == TableDataInfo.class){   //返回类型为TableDataInfo
            //定义包装类Model
            ModelImpl model = new ModelImpl();

            //1、添加code
            Property codeProperty = new StringProperty();
            codeProperty.setDescription(codeName);
            model.property(code,codeProperty);
            //2、添加msg
            Property msgProperty = new StringProperty();
            msgProperty.setDescription(msgName);
            model.property(msg,msgProperty);
            //3.添加total
            Property totalProperty = new StringProperty();
            totalProperty.setDescription(totalName);
            model.property(total,totalProperty);
            //4.添加数据列表
            Property rowsProperty =  new RefProperty(extraKey+ApiResponseBodyReader.swaggerFuffix);
            rowsProperty.setDescription(rowsName);
            model.property(rows,rowsProperty);

            returnValue.put(extraKey,model);
        }else {
            //其他返回值类型如：List、Map 等
            Model model = returnValue.get(extraKey+ApiResponseBodyReader.swaggerFuffix);
            returnValue.put(extraKey,model);
            returnValue.remove(extraKey+ApiResponseBodyReader.swaggerFuffix);
        }

    }
}

